package com.szp.resume.controller;


import com.szp.resume.pojo.ResumeDO;
import com.szp.resume.service.ResumeService;
import flexjson.JSONDeserializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("resume")
public class ResumeController {
    @Autowired
    ResumeService resumeService;

    @RequestMapping("/findAll")
    public List<ResumeDO> findAll(@RequestParam(value="key",required = false) String key) {
        return resumeService.findAll(key);
    }

    @RequestMapping("/findById")
    public ResumeDO findById(long id) {
        return resumeService.findOne(id);
    }

    @RequestMapping("/update")
    public void update(List<ResumeDO> resumes) {
        resumeService.update(resumes);
    }

    @RequestMapping("/save")
    public void save(String data) {
        JSONDeserializer deserializer = new JSONDeserializer();
        List resumeVOS = (ArrayList)deserializer.deserialize(data);
////        List<ResumeDO> added = new ArrayList<>();
//        List<ResumeDO> modified = new ArrayList<>();
//
//        resumeVOS.forEach((resumeVO -> {
//            ResumeDO resumeDO = new ResumeDO();
//            resumeDO.setAddress(resumeVO.get(new String("address")));
//            resumeDO.setId(id);
//            resumeDO.setName(name);
//            resumeDO.setPhone(phone);
//                modified.add(resumeVO.toResumeDO());
//        }));
        resumeService.save(resumeVOS);

    }

    @RequestMapping("/deleteById")
    public void delete(String id) {
        String[] ids = id.split(",");
        for (int i = 0; i < ids.length; i++) {
            long idl = Long.parseLong(ids[i]);
            resumeService.delete(idl);
        }
    }

}
